--
-- Definition of procedure `usp_delete_board`
--

CREATE  PROCEDURE `usp_delete_board`(v_BoardID VARCHAR(64))
BEGIN

DECLARE v_cntpost, v_cnttopic, v_cntforum, v_cntcategory int;
DECLARE v_PostID, v_TopicID, v_ForumID, v_CategoryID VARCHAR(64);

SET v_cntpost:= (SELECT COUNT(p.postid)
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

IF v_cntpost > 0 THEN

SET v_PostID:= (SELECT p.postid
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

DELETE FROM forum_post WHERE PostID = v_PostID;
DELETE FROM item_resources WHERE itemid = v_PostID;
DELETE FROM subscriptions WHERE itemid = v_PostID;
DELETE FROM views WHERE itemid = v_PostID;
DELETE FROM ranks WHERE itemid = v_PostID;
DELETE FROM tags WHERE itemid = v_PostID;
DELETE FROM comments WHERE itemid = v_PostID;
DELETE FROM Options WHERE itemid = v_PostID;
DELETE FROM security WHERE secureitemid = v_PostID;

END IF;

SET v_cnttopic:= (SELECT COUNT(t.topicid)
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

IF v_cnttopic > 0 THEN

SET v_TopicID:= (SELECT t.topicid
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

DELETE FROM forum_topic WHERE TopicID = v_TopicID;

END IF;

SET v_cntforum:= (SELECT COUNT(f.forumid)
FROM forum_forum as f
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

IF v_cntforum > 0 THEN

SET v_ForumID:= (SELECT f.forumid
FROM forum_forum as f
JOIN forum_category as c on c.categoryid = f.categoryid
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

DELETE FROM forum_forum WHERE ForumID = v_ForumID;

END IF;

SET v_cntcategory:= (SELECT COUNT(c.categoryid)
FROM forum_category as c
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

IF v_cntcategory > 0 THEN

SET v_CategoryID:= (SELECT c.categoryid
FROM forum_category as c
JOIN forum_board as b on b.boardid = c.boardid
WHERE b.boardid = v_BoardID);

DELETE FROM forum_category WHERE CategoryID = v_CategoryID;

END IF;

DELETE FROM forum_board WHERE BoardID = v_BoardID;

END;

--GO

--
-- Definition of procedure `usp_delete_category`
--

CREATE  PROCEDURE `usp_delete_category`(v_categoryid VARCHAR(64))
BEGIN

DECLARE v_cntpost, v_cnttopic, v_cntforum int;
DECLARE v_PostID, v_TopicID, v_ForumID VARCHAR(64);

SET v_cntpost:= (SELECT COUNT(p.postid)
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

IF v_cntpost > 0 THEN

SET v_PostID:= (SELECT p.postid
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

DELETE FROM forum_post WHERE PostID = v_PostID;
DELETE FROM item_resources WHERE itemid = v_PostID;
DELETE FROM subscriptions WHERE itemid = v_PostID;
DELETE FROM views WHERE itemid = v_PostID;
DELETE FROM ranks WHERE itemid = v_PostID;
DELETE FROM tags WHERE itemid = v_PostID;
DELETE FROM comments WHERE itemid = v_PostID;
DELETE FROM Options WHERE itemid = v_PostID;
DELETE FROM security WHERE secureitemid = v_PostID;

END IF;

SET v_cnttopic:= (SELECT COUNT(t.topicid)
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

IF v_cnttopic > 0 THEN

SET v_TopicID:= (SELECT t.topicid
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

DELETE FROM forum_topic WHERE TopicID = v_TopicID;

END IF;

SET v_cntforum:= (SELECT COUNT(f.forumid)
FROM forum_forum as f
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

IF v_cntforum > 0 THEN

SET v_ForumID:= (SELECT f.forumid
FROM forum_forum as f
JOIN forum_category as c on c.categoryid = f.categoryid
WHERE c.categoryid = v_CategoryID);

DELETE FROM forum_forum WHERE ForumID = v_ForumID;

END IF;

DELETE FROM forum_category WHERE CategoryID = v_CategoryID;

END;

--GO

--
-- Definition of procedure `usp_delete_forum`
--

CREATE  PROCEDURE `usp_delete_forum`(v_ForumID VARCHAR(64))
BEGIN

DECLARE v_cntpost, v_cnttopic int;
DECLARE v_PostID, v_TopicID VARCHAR(64);

SET v_cntpost:= (SELECT COUNT(p.postid)
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
WHERE f.forumid = v_ForumID);

IF v_cntpost > 0 THEN  

SET v_PostID:= (SELECT p.postid
FROM forum_post as p
JOIN forum_topic as t on t.topicid = p.topicid
JOIN forum_forum as f on f.forumid = t.forumid
WHERE f.forumid = v_ForumID);

DELETE FROM forum_post WHERE PostID = v_PostID;
DELETE FROM item_resources WHERE itemid = v_PostID;
DELETE FROM subscriptions WHERE itemid = v_PostID;
DELETE FROM views WHERE itemid = v_PostID;
DELETE FROM ranks WHERE itemid = v_PostID;
DELETE FROM tags WHERE itemid = v_PostID;
DELETE FROM comments WHERE itemid = v_PostID;
DELETE FROM Options WHERE itemid = v_PostID;
DELETE FROM security WHERE secureitemid = v_PostID;

END IF;

SET v_cnttopic:= (SELECT COUNT(t.topicid)
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
WHERE f.forumid = v_ForumID);

IF v_cnttopic > 0 THEN

SET v_TopicID:= (SELECT t.topicid
FROM forum_topic as t
JOIN forum_forum as f on f.forumid = t.forumid
WHERE f.forumid = v_ForumID);

DELETE FROM forum_topic WHERE TopicID = v_TopicID;

END IF;

DELETE FROM forum_forum WHERE ForumID = v_ForumID;

END;

--GO

--
-- Definition of procedure `usp_delete_templateheader`
--

CREATE  PROCEDURE `usp_delete_templateheader`(v_templateid VARCHAR(64))
BEGIN
            DECLARE v_cnt int;
            
            SET v_cnt:=(SELECT fn_template_usage_count(templateid) FROM template_header WHERE templateid = v_templateid);
            
            IF v_cnt = 0 THEN
            	DELETE FROM template_header WHERE templateid = v_templateid;            
            	DELETE FROM templates	WHERE templateid = v_templateid;
            END IF;
END;

--GO

--
-- Definition of procedure `usp_get_editorscreendetails`
--

CREATE  PROCEDURE `usp_get_editorscreendetails`(v_pageid VARCHAR(64))
BEGIN

DECLARE v_CNT int;
DECLARE v_templateid VARCHAR(64);

SET v_templateid:= (SELECT templateid
FROM page
WHERE pageid = v_pageid);

SET v_CNT:= (SELECT COUNT(*)
FROM page_working_data
WHERE pageid = v_pageid);

IF v_CNT = 0 THEN

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pwd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_working_data as pwd on pwd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
ORDER BY t.placeholdernumber;

ELSE

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pwd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_working_data as pwd on pwd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and pwd.placeholdernumber = t.placeholdernumber
ORDER BY t.placeholdernumber;

END IF;

END;

--GO

--
-- Definition of procedure `usp_get_pagearchivedetails`
--

CREATE  PROCEDURE `usp_get_pagearchivedetails`(v_pageid VARCHAR(64))
BEGIN

DECLARE v_CNT int;
DECLARE v_templateid VARCHAR(64);

SET v_templateid:= (SELECT templateid
FROM page
WHERE pageid = v_pageid);

SET v_CNT:= (SELECT COUNT(*)
FROM page_archive_data
WHERE pageid = v_pageid);

IF v_CNT = 0 THEN

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pad.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_archive_data as pad on pad.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and length(pad.placeholderdata) > 5
and pad.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

ELSE

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pad.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_archive_data as pad on pad.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and pad.placeholdernumber = t.placeholdernumber
and length(pad.placeholderdata) > 5
and pad.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

END IF;

END;

--GO

--
-- Definition of procedure `usp_get_pagefinaldetails`
--

CREATE  PROCEDURE `usp_get_pagefinaldetails`(v_pageid VARCHAR(64))
BEGIN

DECLARE v_CNT int;
DECLARE v_templateid VARCHAR(64);

SET v_templateid:= (SELECT templateid
FROM page
WHERE pageid = v_pageid);

SET v_CNT:= (SELECT COUNT(*)
FROM page_final_data
WHERE pageid = v_pageid);

IF v_CNT = 0 THEN

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pfd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_final_data as pfd on pfd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and length(pfd.placeholderdata) > 5
and pfd.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

ELSE

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pfd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_final_data as pfd on pfd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and pfd.placeholdernumber = t.placeholdernumber
and length(pfd.placeholderdata) > 5
and pfd.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

END IF;

END;

--GO

--
-- Definition of procedure `usp_get_pagepreviewdetails`
--

CREATE  PROCEDURE `usp_get_pagepreviewdetails`(v_pageid VARCHAR(64))
BEGIN

DECLARE v_CNT int;
DECLARE v_templateid VARCHAR(64);

SET v_templateid:= (SELECT templateid
FROM page
WHERE pageid = v_pageid);

SET v_CNT:= (SELECT COUNT(*)
FROM page_working_data
WHERE pageid = v_pageid);

IF v_CNT = 0 THEN

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pwd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_working_data as pwd on pwd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and length(pwd.placeholderdata) > 5
and pwd.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

ELSE

SELECT
RTRIM(th.templatename) as templatename,
th.numberofplaceholders,
t.placeholdernumber,
RTRIM(t.fieldname) as fieldname,
t.fieldtype,
t.fieldheight,
t.fieldwidth,
page.title,
pwd.placeholderdata,
t.id as templatefieldid,
t.showname
FROM template_header as th
LEFT JOIN templates as t on t.templateid = th.templateid
LEFT JOIN page as page on page.templateid = t.templateid
LEFT JOIN page_working_data as pwd on pwd.pageid = page.pageid
WHERE t.templateid = v_templateid
and page.pageid = v_pageid
and pwd.placeholdernumber = t.placeholdernumber
and length(pwd.placeholderdata) > 5
and pwd.placeholderdata <> '<P>&nbsp;</P>'
ORDER BY t.placeholdernumber;

END IF;

END;

--GO

--
-- Definition of procedure `usp_insert_insertpageworkingdata`
--

CREATE  PROCEDURE `usp_insert_insertpageworkingdata`(v_pageid VARCHAR(64), v_placeholdernumber INT, v_placeholderdata TEXT, v_lastuserid VARCHAR(64), v_pagerevision INT)
BEGIN

DECLARE v_CNT int;

SET v_CNT:= (SELECT COUNT(*) FROM page_working_data WHERE pageid = v_pageid and placeholdernumber = v_placeholdernumber);

IF v_CNT = 0 THEN

INSERT INTO page_working_data
(
pageid,
placeholdernumber,
placeholderdata,
lastuserid,
pagerevision
)
VALUES
(
v_pageid,
v_placeholdernumber,
v_placeholderdata,
v_lastuserid,
v_pagerevision
);

ELSE

UPDATE page_working_data
SET	placeholdernumber = v_placeholdernumber,
placeholderdata = v_placeholderdata,
pagerevision = v_pagerevision,
lastuserid = v_lastuserid
WHERE pageid = v_pageid and placeholdernumber = v_placeholdernumber;

END IF;

END;

--GO

--
-- Definition of procedure `usp_insert_menu`
--

CREATE  PROCEDURE `usp_insert_menu`(v_id INT, v_title TEXT, v_description TEXT, v_url TEXT, v_roles TEXT, v_parent INT)
BEGIN

    IF v_parent Is Null THEN
    	UPDATE sitemap
    	SET title = v_title,
    	description = v_description,
    	url = v_url,
    	roles = v_roles
    	WHERE id = v_id;
    ELSE
    	UPDATE sitemap
    	SET title = v_title,
    	description = v_description,
    	url = v_url,
    	roles = v_roles,
    	parent = v_parent
    	WHERE id = v_id;
    END IF;

END;

--GO

--
-- Definition of procedure `usp_insert_rank`
--

CREATE  PROCEDURE `usp_insert_rank`(v_id VARCHAR(64), v_itemid VARCHAR(64), v_userid VARCHAR(64), v_rank int, v_revision int)
BEGIN

                DECLARE v_currank, v_cnt, v_newrank,v_rev int;

                SET v_rev:=(SELECT revision FROM ranks WHERE itemid = v_itemid);
                SET v_currank:=(SELECT AVG(rank) FROM ranks WHERE itemid = v_itemid and userid = v_userid);
                SET v_cnt:=(SELECT count(*) FROM ranks WHERE itemid = v_itemid);

                	IF v_cnt = 0 THEN
                		SET v_newrank = v_rank;
                	ELSE
                		SET v_newrank = avg(v_currank + v_rank);
                	END IF;

                	IF v_cnt = 0 THEN
                		INSERT INTO ranks
                		(id, itemid, userid, rank, revision)
                		VALUES
                		(v_id, v_itemid, v_userid, v_newrank,v_revision);
                	ELSE
                		UPDATE ranks SET rank = v_newrank, revision =v_revision WHERE itemid = v_itemid and userid = v_userid;
                	END IF;

/*
                IF v_rev Is Null THEN


                END IF;*/
END;

--GO

--
-- Definition of procedure `usp_insert_template`
--

CREATE  PROCEDURE `usp_insert_template`(v_id VARCHAR(64), v_templateid VARCHAR(64), v_placeholdernumber INT, v_fieldname TEXT, v_fieldtype INT, _fieldheight INT, v_fieldwidth INT, v_editortype INT, v_ShowName BOOL)
BEGIN

        DECLARE v_placeholderid int;

        SET v_placeholderid:=(SELECT (Max(placeholdernumber) + 1) FROM templates WHERE templateid = v_templateid);

        IF v_placeholderid Is Null THEN
        	SET v_placeholdernumber = 1;
        ELSE
        	SET v_placeholdernumber = v_placeholderid;
        END IF;

        INSERT INTO templates
        (
        	id,
        	templateid,
        	placeholdernumber,
        	fieldname,
        	fieldtype,
        	fieldheight,
        	fieldwidth,
        	editortype,
        	showname
        )
        VALUES
        (
        	v_id,
        	v_templateid,
        	v_placeholdernumber,
        	v_fieldname,
        	v_fieldtype,
        	v_fieldheight,
        	v_fieldwidth,
        	v_editortype,
        	v_showname
        );

END;

--GO

--
-- Definition of procedure `usp_list_boards_bytype`
--

CREATE  PROCEDURE `usp_list_boards_bytype`(v_ListType INT)
BEGIN

IF v_ListType = 0 THEN

SELECT DISTINCT
	BoardID,
	BoardName,
	SortOrder,
	lastuserid,
	LastPostDate,
	fn_post_get_categoryid_byboard(boardid) as categoryid
FROM forum_board;

ELSE

SELECT DISTINCT
	b.BoardID,
	b.BoardName,
	b.SortOrder,
	b.lastuserid,
	b.LastPostDate,
	fn_post_get_categoryid_byboard(b.boardid) as categoryid
FROM forum_board as b
left join forum_category as c on c.boardid = b.boardid
WHERE c.categoryid Is Not Null;

END IF;

END;

--GO

--
-- Definition of procedure `usp_movepagedata_tolive`
--

CREATE  PROCEDURE `usp_movepagedata_tolive`(v_pageid VARCHAR(64))
    MODIFIES SQL DATA
BEGIN

DECLARE v_pagerevision BIGINT;
DECLARE done INT DEFAULT 0;
DECLARE v_pageidtmp, v_lastuseridtmp VARCHAR(64);
DECLARE v_placeholdernumtmp, v_pagerevisiontmp, v_cnt  INT DEFAULT 0;
DECLARE v_placeholderdatatmp TEXT;
DECLARE cursor1 cursor FOR SELECT pageid, placeholdernumber, placeholderdata, lastuserid, pagerevision FROM page_working_data WHERE pageid = v_pageid;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

SET v_pagerevision = 0;

SET v_pagerevision:= (SELECT (max(pagerevision)+1) FROM page WHERE pageid = v_pageid);
UPDATE page SET pagerevision=v_pagerevision, iv_status=6 WHERE pageid = v_pageid;
UPDATE page_working_data SET pagerevision=v_pagerevision WHERE pageid = v_pageid;

OPEN cursor1;

REPEAT

fetch cursor1 INTO v_pageidtmp, v_placeholdernumtmp, v_placeholderdatatmp, v_lastuseridtmp, v_pagerevisiontmp;

IF NOT done THEN

SET v_cnt:=(SELECT COUNT(*) FROM page_final_data WHERE pageid = v_pageidtmp AND placeholdernumber = v_placeholdernumtmp);

IF v_cnt = 0 THEN
	INSERT INTO page_final_data
	(pageid, placeholdernumber, placeholderdata, lastuserid, pagerevision)
	VALUES
	(v_pageidtmp, v_placeholdernumtmp, v_placeholderdatatmp, v_lastuseridtmp, v_pagerevisiontmp);
ELSE
	UPDATE page_final_data SET	placeholdernumber = v_placeholdernumtmp,
		placeholderdata = v_placeholderdatatmp,
		pagerevision = v_pagerevisiontmp,
		lastuserid = v_lastuseridtmp
	WHERE pageid = v_pageidtmp AND placeholdernumber = v_placeholdernumtmp;
END IF;

END IF;

UNTIL done
END REPEAT;

CLOSE cursor1;

INSERT INTO page_archive_data
(id, pageid, modifydate, placeholdernumber, placeholderdata, lastuserid, pagerevision)
SELECT
	UUID(),
	pageid,
	modifydate,
	placeholdernumber,
	placeholderdata,
	lastuserid,
	pagerevision
FROM page_final_data
WHERE pageid = v_pageid;

END;

--GO

--
-- Definition of procedure `usp_update_folder`
--

CREATE  PROCEDURE `usp_update_folder`(v_nodeid VARCHAR(64), v_parentnodeid VARCHAR(64), v_name TEXT)
BEGIN

    IF v_parentnodeid Is Null THEN
    	UPDATE folders 
    	SET name = v_name
    	WHERE nodeid = v_nodeid;
    ELSE
    	UPDATE folders 
    	SET parentnodeid = v_parentnodeid, name = v_name
    	WHERE nodeid = v_nodeid;
    END IF;

END;

--GO

--
-- Definition of procedure `usp_update_viewcount`
--

CREATE  PROCEDURE `usp_update_viewcount`(v_itemid VARCHAR(64))
BEGIN

DECLARE v_data BIGINT;
SET v_data = null;

SET v_data:= (SELECT (MAX(viewcount) + 1) FROM views WHERE itemid = v_itemid);

IF v_data > 1 THEN
UPDATE views SET viewcount = v_data  WHERE itemid = v_itemid;
ELSE
INSERT INTO views (itemid, viewcount) VALUES (v_itemid, 1);
END IF;

END;
